//Nathan Popham
//4003-573-01: Procedural Shading
//Lab 4 
//
//mybrick.vert
// Textures a surface with a pattern of 1x1, 1x2, and 2x2 blocks. A predefined
// 5x5 pattern of these blocks is repeated across the surface. The surface is
// bumpmapped over a noise function. The surface color is also varied according
// to a noise function.
//

vec3 calculateTangent (vec3 normal)
{
	vec3 tangent;
	vec3 c1 = cross(normal, vec3(0.0, 0.0, 1.0)); 
	vec3 c2 = cross(normal, vec3(0.0, 1.0, 0.0)); 
	
	if(length(c1)>length(c2))
	{
		tangent = c1;	
	}
	else
	{
		tangent = c2;	
	}
	
	tangent = normalize(tangent);

	return tangent;
}

varying vec3 P;
varying vec3 N;
varying vec3 T;
varying vec3 B;
varying vec3 LightDir;
varying vec3 EyeDir;

void main()
{
   gl_TexCoord[0] = gl_MultiTexCoord0;
   P = gl_Vertex.xyz;

   N = normalize(gl_NormalMatrix * gl_Normal); 
   T = calculateTangent(N);
   B = cross(N, T);

   
   vec3 temp;
   if(gl_LightSource[0].position[3] == 0.0)
        LightDir = -normalize(vec3((gl_LightSource[0].position)));
   else
        LightDir = normalize(vec3(gl_LightSource[0].position) - P);   
    
    
    EyeDir = vec3(gl_ModelViewMatrix * gl_Vertex);
    temp.x = dot(EyeDir, T);
    temp.y = dot(EyeDir, B);
    temp.z = dot(EyeDir, N);
    EyeDir = normalize(temp);

       

   // Write your code here
   gl_Position = ftransform();
}
